<?php

namespace app\index\model;

use think\Db;

/**
 * @author xjy
 */
class NewskuModel
{

    static public function getNewSkuList($param, $titles = null, $function = null)
    {
        $db = Db::table('skus')->alias('a');
        !empty($param['sku']) && $db->where('a.sku', 'in', explodeStr($param['sku']));
        !empty($param['spu']) && $db->where('a.spu', 'in', explodeStr($param['spu']));
        !empty($param['sales_status']) && $db->where('a.sales_status', 'in', explodeStr($param['sales_status']));
        if(isset($param['status'])){
            if($param['status']==='0'||!empty($param['status'])){
                $db->where('a.status', $param['status']);
            }
        }
        if (!empty($param['creation_time'])) {
            $time = explode(' - ', $param['creation_time']);
            $db->where('a.creation_time', '>=', $time[0])->where('a.creation_time', '<=', $time[1]);
        }
        $db->order('id','desc');
        if(!isset($param['sku'])||($param['sku']===''&&$param['spu']===''&&$param['creation_time']===''&&$param['sales_status']===''&&$param['status']==='')){
            $param['_cache']="getNewSkuListCount";
        }
        return \app\common\service\ToolService::downOrReturn($db, $param, $titles, $function);
    }
    static public function getCateById($id){
        if(empty($id)) return [];
        $id = is_array($id)?$id:[$id];
        return Db::table('common_categories')->where(['id'=>['in',$id]])
            ->column('category','id');
    }
    static public function getSkuBySku($sku){
        if(empty($sku)) return [];
        $sku = is_array($sku)?$sku:[$sku];
        return Db::table('skus')->where(['sku'=>['in',$sku]])
            ->column('name','sku');
    }

}
